﻿using BPMN.Application.Contracts.DTO.Request.Plan;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Scheduling;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    public class PlanAppService : IPlanAppService
    {
        public PlanAppService(IRepositoryBasetow<Plan> plan,IRepositoryBasetow<Plan_type> plan_type)
        {
            Plan = plan;
            Plan_Type = plan_type;
        }

        public IRepositoryBasetow<Plan> Plan { get; }
        public IRepositoryBasetow<Plan_type> Plan_Type { get; }

        public bool AddPlan(Plan plan)
        {
            return Plan.Add(plan);
        }

        public bool DeletePlan(int id)
        {
            return Plan.Delete(id);
        }

        public List<PlanDto> GetPlan(string plan_name, int pageIndex, int pageSize, out int totalCount, out int pagerCount)
        {
           var list=from p in Plan.GetAll()
                    join pt in Plan_Type.GetAll() on p.plan_type_Id equals pt.plan_type_Id
                    where(p.IsDel==false)
                    select new PlanDto
                    {
                        plan_Id = p.plan_Id,
                        plan_Name = p.plan_Name,
                        plan_encode = p.plan_encode,
                        shifts = p.shifts,
                        Shift = p.Shift,
                        Remark = p.Remark,
                        plan_type_Name = pt.plan_type_Name,
                        IsDel = p.IsDel,
                        State = p.State,
                        Stime = p.Stime,
                        Etime = p.Etime
                        
                    };
            if (!string.IsNullOrEmpty(plan_name))
            {
                list = list.Where(x => x.plan_Name.Contains(plan_name));
            }
            totalCount=list.Count();
            pagerCount = (int)Math.Ceiling(totalCount * 1.0) / pageSize;
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

        public Plan GetPlanById(int id)
        {
           return Plan.GetAll().FirstOrDefault(x=>x.plan_Id==id);
        }

        public List<Plan_type> GetPlan_Type()
        {
            return Plan_Type.GetAll().ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool plDelete(List<long> ids)
        {
            var list = Plan.GetAll();
            foreach (var item in list.ToList())
            {
                foreach (var id in ids)
                {
                    if (item.plan_Id == id)
                    {
                        item.IsDel = true;
                    }
                }
            }
            Plan.SaveChanges();
            return true;
        }

        public bool UpdatePlan(Plan plan)
        {
           return Plan.Update(plan);
        }
    }
}
